package controller;

import DAO.BaseDAO;
import DAO.CategoryDAO;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpSession;
import model.Category;
import model.User;
import org.apache.commons.mail.EmailException;
import org.primefaces.context.RequestContext;
import validator.e;

@ManagedBean(name = "categoryController")
@SessionScoped
public class CategoryController {

    public Category category;
    public List lista;
    public DataModel listCategory;

    public CategoryController() {
        category = new Category();
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public void saveCategory() throws EmailException {
        BaseDAO dao = new CategoryDAO();
        dao.save(category);
    }

    public DataModel getListCategory() {
        List<Category> list = new CategoryDAO().list();
        listCategory = new ListDataModel(list);
        return listCategory;
    }

    public String prepareUpdate() {
        category = (Category) (listCategory.getRowData());
        return "updateCategory";
    }

    public String updateCategory() {
        BaseDAO dao = new CategoryDAO();
        dao.update(category);
        setCategory(new Category());
        return "consultCategory";
    }

    public String removeCategory() {

        Category categoryTemp = (Category) (listCategory.getRowData());
        BaseDAO dao = new CategoryDAO();
        dao.remove(categoryTemp);
        return "#";
    }

    public void clearCategory() {
        category = new Category();
    }
}
